<?php
namespace app\home\controller;
use app\home\model\TopicnewsModel;
use \think\Controller;
use \think\Request;
use \think\Session;
use app\home\model\CommunityModel;
use app\home\model\TopicsModel;
class Topics extends Controller
{
    /**
     * 话题列表
     */
    public function TopicDetails($id){
        $id = input('id');
        $user_id = Session::get('USER.id');
        $communityDetails = new CommunityModel();
        $communityDetails = $communityDetails->communityDetails($id);
        $topics = new TopicsModel();
        $topics = $topics->topics($id);
        foreach($topics as $key => $value){
            $topics_id = $value['id'];
            $topicCommentDetailsCount = new TopicnewsModel();
            $topicCommentDetailsCount  = $topicCommentDetailsCount->topicCommentDetailsCount($topics_id);
            $topics[$key]['topicCommentDetailsCount'] = $topicCommentDetailsCount;
        }
        $this->assign('topics', $topics);
        $this->assign('communityDetails', $communityDetails);
        $this->assign('user_id', $user_id);
        return $this->fetch('details');
    }

    /**
     * 写话题
     */
    public function detailsInfo(){
        $id = input('id');
        $communityDetails = new CommunityModel();
        $communityDetails = $communityDetails->communityDetails($id);
        $this->assign('community_id', $id);
        $this->assign('communityDetails', $communityDetails);
        return $this->fetch('details_info');
    }

    /**
     * 发表话题
     */
    public function detailsTopic(){
        if(Session::get('USER.id') == ''){
            return 'login';
        }
        $community_id = input('community_id');
        $title = input('title');
        $topic_url = input('topic_url');
        $topic_content = input('topic_content');
        if($title == ''){
            return 'empty_title';
        }
        if($topic_content == ''){
            return 'empty_content';
        }
        if($title != '' && $topic_content != ''){
            $request = Request::instance();
            $data = [
                'community_id' => $community_id,
                'user_id' => Session::get('USER.id'),
                'title' => $title,
                'topic_url' => isset($topic_url)?$topic_url:'',
                'topic_content' => $topic_content,
                'report_time' => time(),
                'report_ip' => $request->ip()
            ];
            $addTopics = new TopicsModel();
            $addTopics = $addTopics->addTopics($data);
            return $addTopics;
        }
    }

    /**
     * 发表话题图片
     */
    public function upload(){
        $file = request()->file('img');
        $info = $file->move(ROOT_PATH . 'public' . DS . 'upload'.DS.'topics');
        if($info){
            echo $path ='upload'.'/'.'topics'.'/'.date('Ymd').'/'.$info->getFilename();
        }else{
            echo $file->getError();
        }
    }

    /**
     * 删除话题
     */
    public function topicDel(){
        $id = input('id');
        $topic_del = new TopicsModel();
        $topic_del = $topic_del->topicDel($id);
        return $topic_del;
    }


}